package com.qf.controller;

import com.qf.dao.DaoImpl.UserDaoImpl;
import com.qf.pojo.User;
import com.qf.service.serviceImpl.UserServiceImpl;
import com.qf.util.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/27 19:18
 */
@WebServlet("/User")
public class UserController extends BaseService{
    public String login(HttpServletRequest request, HttpServletResponse response){

        //获取前端 输入的 用户名 与 密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //判断 用户名 与 密码 是否为空
        if (password == null || "".equals(password)) {
            return Constants.REDIRECT + request.getContextPath() + "/login.jsp";
        }
        if (username == null || "".equals(username)) {
            return Constants.REDIRECT + request.getContextPath() + "/login.jsp";
        }

        //调用 service 层
        UserDaoImpl userDao = new UserDaoImpl();
        UserServiceImpl userService = new UserServiceImpl(userDao);
        User user = userService.selectUser(username, password);
        //判断查出的对象是否为null
        if (user == null) {
            return Constants.REDIRECT + request.getContextPath() + "/login.jsp";
        }else {
            request.getSession().setAttribute("username",username);
            return  Constants.REDIRECT + request.getContextPath() + "/show?method=showList";
        }
    }
    public String  registered(HttpServletRequest request, HttpServletResponse response){

        //获取前端 输入的 用户名 与 密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //判断 用户名 与 密码 是否为空
        if (password == null || "".equals(password)) {
            return Constants.REDIRECT + request.getContextPath() + "/login.jsp";
        }
        if (username == null || "".equals(username)) {
            return Constants.REDIRECT + request.getContextPath() + "/login.jsp";
        }

        //调用 service 层
        UserDaoImpl userDao = new UserDaoImpl();
        UserServiceImpl userService = new UserServiceImpl(userDao);
        boolean results  = userService.insertUser(username, password);
        if (results) {
            return Constants.REDIRECT + request.getContextPath() + "/login.jsp";
        }else {
            return Constants.REDIRECT + request.getContextPath() + "/registered.jsp";
        }
    }


}
